﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndoReputation.Models.BusinessLogic;
using Telerik.Web.Mvc;
using IndoReputation.Views.ViewModels;
using IndoReputation.Models.BusinessObject;
using IndoReputation.Models.BusinessObject.Collections;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class OfficeController : BaseController
    {
        //
        // GET: /Office/

        public ActionResult Index()
        {
            ViewData["ajax"] = true;
            ViewData["scrolling"] = true;
            ViewData["paging"] = true;
            ViewData["filtering"] = true;
            ViewData["grouping"] = true;
            ViewData["sorting"] = true;
            ViewData["showFooter"] = true;
            return View();
            //OfficeBOL.GetAllOfficeJoin()
        }

        [GridAction]
        public ActionResult _Index()
        {
            return View(new GridModel(OfficeBOL.GetAllOfficeJoin()));
        }

        [HttpGet]
        public ActionResult Add()
        {
            var officeType = OfficeTypeBOL.GetAllOfficeType();
            var country = CountryBOL.GetAllCountry();
            var model = new OfficeViewModel
            {
                OfficeType = new SelectList(officeType, "ID", "Name"),
                Country = new SelectList(country, "ID", "Name")
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Add(OfficeViewModel model)
        {
            if (ModelState.IsValid)
            {
                Office office = new Office
                {
                    CityId = model.CityId,
                    Description = model.Description,
                    IsEnable = model.IsEnable,
                    Name = model.Name.Trim(),
                    OfficeTypeID = model.OfficeTypeID                    
                };
                OfficeBOL.Insert(office);
            }
            return RedirectToAction("Index");
        }

        public JsonResult _GetDropDownListCity(int? CountryID)
        {
            CityList listCity = new CityList();
            if (CountryID.HasValue)
            {
                listCity = CityBOL.GetAllCityByCountryID(CountryID ?? 0);
            }
            return Json(new SelectList(listCity, "ID", "Name"), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Edit(int officeID)
        {
            var office = OfficeBOL.GetOfficeJoinGroupById(officeID);
            var officeType = OfficeTypeBOL.GetAllOfficeType();
            var country = CountryBOL.GetAllCountry();
            var city = CityBOL.GetAllCityByCountryID(office.CountryID);            
            var model = new OfficeViewModel
            {
                City = new SelectList(city, "ID", "Name"),
                CityId = office.CityId,
                Country = new SelectList(country, "ID", "Name"),
                CountryID = office.CountryID,
                CreateAt = office.CreateAt,
                CreateBy = office.CreateBy,
                Description = office.Description,
                ID = office.ID,
                IsEnable = office.IsEnable,
                Name = office.Name,                
                OfficeType = new SelectList(officeType, "ID", "Name"),
                OfficeTypeID = office.OfficeTypeID                
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(OfficeViewModel model)
        {
            if (ModelState.IsValid)
            {
                Office office = new Office
                {
                    CityId = model.CityId,
                    CreateAt = model.CreateAt,
                    CreateBy = model.CreateBy,
                    Description = model.Description,
                    ID = model.ID,
                    IsEnable = model.IsEnable,
                    Name = model.Name.Trim(),
                    OfficeTypeID = model.OfficeTypeID                    
                };
                OfficeBOL.Update(office);
            }
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditing(int id)
        {
            //Find a customer with ProductID equal to the id action parameter            
            if (id > 0)
            {
                //Delete the record
                if (!OfficeBOL.Delete(id))
                {
                    ModelState.AddModelError(String.Empty, @"This Office have Users or Office CEO. Please check again!");
                }
            }
            //Rebind the grid
            return View(new GridModel(OfficeBOL.GetAllOfficeJoin()));
        }
    }
}
